package com.ruoyi.web.controller.zkcontroller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.web.domain.User;
import com.ruoyi.web.service.zkService.UserService;
import com.ruoyi.web.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(){
        return R.ok("默认验证码为258196");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession httpSession){

        log.info("获取的数据是{}",map);
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        if(!code.equals("258196")){
            return R.error("验证码错误");
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        if(user==null){
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        httpSession.setAttribute("user",user.getId());

        return R.ok(user);
    }
}

